import java.io.Serializable;
import java.util.ArrayList;

import org.omg.CORBA.OMGVMCID;


public class Store implements Serializable{
	private ArrayList<Computer> computerList;
	private ArrayList<Customer> customerList;
	
	public Store()
	{
		computerList = new ArrayList<>();
		customerList = new ArrayList<>();
	}
	public int customerExists(String ID)
	{
		for(int i = 0; i < customerList.size(); i++)
		{

			if(customerList.get(i).getCustomerID().equals(ID))
				return i;
		}
		return -1;
	}
	public boolean addCustomer(String other)
	{
		Customer customer = CustomerParser.customerParser(other);
		 for(int i = 0; i < customerList.size(); i++)
		 {
			 if(customerList.get(i).getCustomerID().equals(customer.getCustomerID()))
				 return false;
		 }
		 customerList.add(customer);
		 return true;
	}

	public boolean removeCustomer(String id) {
		for(int i = 0; i < customerList.size(); i++)
		{
			if(customerList.get(i).getCustomerID().equals(id))
			{
				customerList.remove(i);
				return true;
			}
		}
		return false;
	}
	public void sortCustomer() {
		Sorts.Sort(customerList);
	}
	public String listCustomer() {
		String output = "";
		for(int i = 0; i < customerList.size(); i++)
		{
			output += customerList.get(i).toString() + "\n";
		}
		return output;
	}
	public int computerExists(String brandName, String cpuType, String cpuSpeed, String memory)
	{
		for(int i = 0; i < computerList.size(); i++)
		{
			Computer com = computerList.get(i);
			if(com.getBrandName().equals(brandName) && com.getCPU().getType().equals(cpuType) 
					&& com.getCPU().getSpeed().equals(cpuSpeed) && com.getMemory().equals(memory))
				return i;
		}
		return -1;
	}
	public boolean addComputer(String computer)

	{
		Computer com1 = ComputerParser.computerParser(computer);
		for(int i = 0; i < computerList.size(); i++)
		{
			Computer com = computerList.get(i);
			if(com.getBrandName().equals(com1.getBrandName()) && com.getCPU().getType().equals(com1.getCPU().getType()) 
					&& com.getCPU().getSpeed().equals(com1.getCPU().getSpeed()) && com.getMemory().equals(com1.getMemory()))
				
				return false;
		}
		computerList.add(com1);
		return true;
	}
	public boolean removeComputer(String brandName, String cpuType, String cpuSpeed, String memory)
	{

		for(int i = 0; i < computerList.size(); i++)
		{
			Computer com = computerList.get(i);
			if(com.getBrandName().equals(brandName) && com.getCPU().getType().equals(cpuType) 
					&& com.getCPU().getSpeed().equals(cpuSpeed) && com.getMemory().equals(memory))
			{
				computerList.remove(i);
				return true;
			}
		}
		return false;
	}
	void sortComputer()
	{
		Sorts.Sort(computerList);
	}
	public String listComputer() {
		String output = "";
		for(int i = 0; i < computerList.size(); i++)
		{
			output += computerList.get(i).toString() + "\n";
		}
		return output;
	}
	public void closeStore() {
		computerList.clear();
		customerList.clear();
	}
	public boolean writeobj(String filename)
	{
		return IO.writeobj(filename, this);
	}
	
	public boolean readobj(String filename)
	{
		Store newstore = new Store();
		newstore = IO.readobj(filename);
		this.closeStore();
		this.computerList = newstore.getComputerList();
		this.customerList=newstore.getCustomerList();
		if (newstore==null)
			return false;
		else
			return true;
			
	}
	public ArrayList<Computer> getComputerList() {
		return computerList;
	}
	public void setComputerList(ArrayList<Computer> computerList) {
		this.computerList = computerList;
	}
	public ArrayList<Customer> getCustomerList() {
		return customerList;
	}
	public void setCustomerList(ArrayList<Customer> customerList) {
		this.customerList = customerList;
	}
	
}
